<?php
namespace app\index\controller;
use think\Request;
use think\Db;
use think\Controller;
use app\index\model\Menu as Menumodel;
class Shop extends Controller
{
    
    public function mens(Menumodel $mol)//
    {
    	
    	$menu = $mol->menu();
        $username = session('username');
    	$product = $mol->getProducts();
    	$image = $mol->getPictures();//获取图片
    	foreach ($image as $key=>$value){
    		$pid=$value->pid;
    		$image[$key]->image = Db::table('mall_image')->where("pid=$pid")->select();
    	}
      	$this->assign('menu',$menu);
      	$this->assign('product',$image);
        $this->assign('username',$username);
		return $this->fetch();
    }
     public function menued(Request $req ,Menumodel $mol)//查询菜单
    {
     $id = $req->param('id');

     return $mol->selemenu($id);
     //dump(json_encode($mol->selemenu($id)));
    }
      public function mensle(Menumodel $mol,Request $req)//商品详情
    {
    	$pid = $req->param('pid');
        $username = session('username');
    	$menu = $mol->menu();
    	$product = $mol->getProduct($pid);
    	$content = $mol->getContent($pid);
    	foreach($product as $key=>$value)
    	{
    		$pid = $value->pid;
    		$product[$key]->image = Db::table('mall_image')->where("pid=$pid")->select();
    		
    		$product[$key]->content = Db::table('mall_comment')->where("pid=$pid")->select();
    	}
    	$this->assign('menu',$menu);
        $this->assign('username',$username);
    	$this->assign('product',$product);
		return $this->fetch();
    }
    public function collection(Menumodel $mol,Request $req)//点心 收藏
    {
        $username = session('username');
        //return $username;
        $pid = $req->param('pid');
        $uid = $mol->getids($username);
        $uid = $uid[0]->uid;
        $data = ['uid'=>$uid,
                 'pid'=>$pid,
                 'create_time'=>time()
                ];
        //return $uid;
        $result = $mol->add($data);
        if($result){
            session('pid',$pid);
            session('uid',$uid);
            return true;
        }else{
            return false;
        }
    }
}
